Previous Book Contents Book Index Next

Inside Macintosh: QuickDraw GX Programmer's Overview / Part 1 - Getting Started With QuickDraw GX
Chapter 1 - An Introduction to QuickDraw GX


Object-Based Architecture

The graphics, typography, and printing features of QuickDraw GX all rely on an object-based architecture. This section defines what an object is, discusses the different types of objects QuickDraw GX uses, specifies some advantages of an object-based architecture, and discusses how objects are stored in memory.

Objects and Object References

A QuickDraw GX object is a special kind of data structure: a private data structure. Some of the data structures used by the Macintosh Operating System are public--that is, their internal structure is publicly defined and your application can manipulate their fields directly. QuickDraw GX objects, on
the other hand, are private. Their fields are called properties. Your application cannot directly manipulate the properties of an object; instead, you must
call QuickDraw GX functions to determine or to change the value of an
object property.

QuickDraw GX does not provide your application with pointers or handles to objects. Instead, QuickDraw GX provides an object reference value that uniquely identifies a particular object. When you create an object, QuickDraw GX creates an object reference value for that object and returns it to your application. If you want to determine or change the value of one of its properties, you call a QuickDraw GX function, sending the object reference value as a parameter.

QuickDraw GX uses many different types of objects to provide its functionality. Each object type has a unique set of properties, and QuickDraw GX uses each type of object for specific purposes. For example, QuickDraw GX uses the shape object type to represent a single graphics or typographic shape such as
a rectangle, a curve, a line of text, a bitmap, or a picture. The shape object
has nine properties. Each property contains a different kind of value and QuickDraw GX uses the value of each property in a different way.

See Chapter 3, "Using QuickDraw GX Objects," for more information and examples of using object references.

Advantages of Objects

QuickDraw GX uses an object-based architecture, whereas QuickDraw uses a state-based architecture. This section discusses some of the advantages of using this kind of architecture, as well as some of the challenges it presents.

State-Independent Information

The first advantage of an object-based architecture is that it does not rely on state information. Because QuickDraw GX uses objects to represent graphics shapes and also to represent modifications to those shapes, each shape object contains or references the information necessary to draw the shape.

QuickDraw, on the other hand, relies on state information to determine how and where to draw graphics. Therefore, if you are drawing two images with different stylistic information, you must set up the drawing environment to draw the first image and then set it up again for the second image, losing the information for the first image.

Since QuickDraw GX stores the stylistic information in an object, each shape can have its own stylistic information, and you can draw two shapes with different stylistic information without having to reset the drawing environment or lose any information.

Caches

A second advantage of an object-based architecture is that it provides a convenient memory location to cache predrawing calculations. Certain calcula-
tions are necessary before drawing any shape. For some shapes, these calculations can take more time than the actual rendering. Because drawing with QuickDraw relies on state information, predrawing calculations typically must be thrown away after drawing each image.

However, with QuickDraw GX, the predrawing calculations for each shape can be stored as a cache associated with the shape object. If memory permits, these caches persist from one drawing of a particular shape to the next. If you draw a shape you have already drawn (which you might do, for example, in response to an update event), QuickDraw GX can use the information in the cache to speed the drawing process.

Although QuickDraw GX creates and maintains these caches for you, it also provides a mechanism for you to create and dispose of them to suit your application's particular needs.

Privacy

A third advantage of an object-based architecture is that it allows for privacy. You do not have direct access to the internals of an object; instead, you must make function calls to examine or modify the values of an object's properties.

As a result, objects behave more consistently, unwanted side effects are minimized, and QuickDraw GX can take care of housekeeping tasks like tracking the current number of references to an object.

Another important result of privacy is that QuickDraw GX can store objects in memory managed by a graphics accelerator--memory that is not necessarily accessible to your application.

Consistency

A fourth advantage of an object-based architecture is that it allows for a consistent programming interface. True object-oriented systems have polymorphism, allowing a single function to operate on a number of different object types. QuickDraw GX mimics polymorphism in two ways:

Sharing

A final advantage of an object-based architecture is that it allows for sharing, which allows for multiple object references and, therefore, reduces the number of objects held in memory at a given time.

Object sharing is one of the ways that QuickDraw GX saves on the amount of memory required to represent complex graphics. Sharing is discussed more fully in the next section, "Objects and Memory." The section, "Sharing Styles" in Chapter 3, "Using QuickDraw GX Objects," gives more information and several examples.

Objects and Memory

The memory demands of the sophisticated, object-based graphics environment of QuickDraw GX are quite different from the memory demands of the state-based environment of QuickDraw. Consequently, QuickDraw GX has
its own memory management architecture--separate from the Macintosh Memory Manager.

The memory QuickDraw GX uses is separate from the memory your application uses. QuickDraw GX may even use memory located on a graphics accelerator card that your application may not be able to address. However, the fact that QuickDraw GX memory is separate is usually hidden from your application because you call QuickDraw GX functions to examine or modify the contents of a QuickDraw GX object.

Object-based systems can use large amounts of memory, especially when an application needs to create and use many objects to represent complex shapes. QuickDraw GX provides two mechanisms to minimize the amount of memory required to render complex graphics: object sharing and object loading and unloading. The next two sections discuss these mechanisms.

Managing Owner Counts

As introduced in the section "Sharing" on page 14, multiple shape objects
can contain references to the same supporting objects which helps reduce memory use.

QuickDraw GX objects have an owner count property, which allows your application to maintain the number of references to a object. When you dispose of an object that is no longer needed, QuickDraw GX decrements the object's owner count. If the result is 0, there are no remaining references to the object,
so the object can be deleted from memory. If the result is 1 or greater, then references to the object still exist and the object is left in memory.

See the section "Cloning and Disposing of Shapes" in Chapter 3 for examples.

Loading and Unloading Objects

Another way that QuickDraw GX minimizes memory requirements is by moving objects back and forth between memory and external storage as needed. If QuickDraw GX needs additional memory to create new objects,
it can unload objects already in memory. When unloaded, an object is moved from computer memory to temporary private storage on disk. When loaded, that object is restored to normal object form in memory.

Typically, QuickDraw GX unloads objects that have not been referenced recently before unloading objects that your application has been using frequently. For shape objects, you can specify whether you want QuickDraw GX to unload the shape before other shapes or after other
shapes when memory is needed.

QuickDraw GX automatically handles the loading and unloading process for you--you do not have to be concerned with or even know whether a shape is loaded or unloaded.

Note
When QuickDraw GX shapes are unloaded, they exist on disk only temporarily. If you want to store an object on disk in a more permanent manner, you must convert the object into a compressed description suitable for storage. This process is called flattening the object. Conveniently, a flattened object is also suitable for interapplication communication. See Inside Macintosh: QuickDraw GX Environment and Utilities for more details.

Previous Book Contents Book Index Next

© Apple Computer, Inc.
6 JUL 1996